package GUI;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.AbstractButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;

import Entity.*;

import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Date;

import javax.swing.JTable;
import java.awt.Panel;
import javax.swing.JCheckBox;

import Entity.Order;

import Controller.Client;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


class OrdersTable1 extends AbstractTableModel
{
	
	
	
	
	String[] titles={"Order ID","Customer ID","Car Number"};
	Object[][] data;
	
	public OrdersTable1(Order[] o)
	{
		super();
		this.data=new Object[o.length][3];
		for(int i=0; i<o.length; i++)
		{
			this.data[i][0]=o[i].getOrderID();
			this.data[i][1]=o[i].getCustomerID();
			this.data[i][2]=o[i].getCarNumber();
		}
	}
	@Override
	public int getColumnCount() {
		return this.titles.length;
	}

	@Override
	public int getRowCount() {
		return data.length;
	}

	@Override
	public Object getValueAt(int arg0, int arg1) {
		return data[arg0][arg1];
	}
	
	@Override
	public String getColumnName(int column) {
		// TODO Auto-generated method stub
		return (String) this.titles[column];
	}
}
/**
 * this is the GUI for viewing the canceled orders.
 * @author Shahar Shelly
 *
 */
public class ViewOrCancelOrders extends JPanel {
	private int row=-1;
	private JTextField OrderID;
	private JTextField CustomerID;
	private JTextField CarNumber;
	private JTextField OrderDate;
	private JTextField ParkingLotNumber;
	private JTextField LeavingDate;
	private JTextField ArrivalDate;
	private JTextField ActualArrivalDate;
	private JTextField ActualLeavingDate;
	private JTable table;
	private JTextField Email;
	JFrame frame;
	JPanel lastPanel;
	public static void cancelOrder(Order order)
	{
		Date curDate=new Date();
		Customer [] customers=Client.getCustomers();
		Customer customer = null;
		for (int i=0; i<customers.length; i++)
			if (customers[i].getcustomer_id().equals(order.getCustomerID()))
				customer=customers[i];
		Prices p=Client.getCurrentPrices(order.getParkingLotNumber());
		double payment=p.getOrderedParkingPlace()*((order.getLeavingDate().getTime()-order.getArrivalDate().getTime())/1000*60*60);
		customer.AddBalance(-payment);
		if (order.isCanceled()==false)
		{
			order.setCanceled(true);
			Client.UpdateOrderInDB(order);
			if ((order.getArrivalDate().getTime()-curDate.getTime()) > 1000*60*60*3)
				JOptionPane.showMessageDialog(null,"The order is canceld,your account were acquitted by "+ payment+"$ (full price)","Cancel order",JOptionPane.INFORMATION_MESSAGE);
			else if ((order.getArrivalDate().getTime()-curDate.getTime()) > 1000*60*60*1)
			{
				payment/=2;
				JOptionPane.showMessageDialog(null,"The order is canceld,your account were acquitted by "+ payment+"$ (half price)","Cancel order",JOptionPane.INFORMATION_MESSAGE);
			}
			else
			{
				payment=0;
				JOptionPane.showMessageDialog(null,"The order is canceld,your account weren't acquitted.","Cancel order",JOptionPane.INFORMATION_MESSAGE);
			}
		}
		else
			JOptionPane.showMessageDialog(null,"the selected order is already canceld","Cancel order",JOptionPane.ERROR_MESSAGE);

	}
	public void setLastPanel (JPanel panel)
	{
		this.lastPanel=panel;
	}
	public void setFrame (JFrame frame)
	{
		this.frame=frame;
	}

	/**
	 *  is the constructor. it has a table to chose from and save after validate.
	 */
	
	public ViewOrCancelOrders(Order[] o) {
		final Order[] o2=o;
		
		final JCheckBox chckbxNewCheckBox = new JCheckBox("Canceled");
		chckbxNewCheckBox.setBounds(0, 285, 85, 23);
		add(chckbxNewCheckBox);
		
		setBounds(100, 100, 580, 343);
		setBorder(new EmptyBorder(5, 5, 5, 5));
		setLayout(null);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frame.setContentPane(lastPanel);
			}
		});
		btnCancel.setBounds(465, 266, 89, 23);
		add(btnCancel);
		
		table = new JTable(new OrdersTable1(o));
		table.setBounds(270, 64, 241, 124);
		JScrollPane sp=new JScrollPane(table);
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
			}
		});
		sp.setBounds(209, 11, 345, 236);
		add(sp);
		
		JPanel OrderDetails = new JPanel();
		OrderDetails.setBorder(new TitledBorder(null, "Order deatails", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		OrderDetails.setBounds(0, 3, 199, 286);
		add(OrderDetails);
		OrderDetails.setLayout(null);
		
		OrderID = new JTextField();
		OrderID.setBounds(82, 17, 107, 20);
		OrderDetails.add(OrderID);
		OrderID.setColumns(10);
		OrderID.setEditable(false);
		
		JLabel lblActualLeavingDate = new JLabel("Actual Leaving Date:");
		lblActualLeavingDate.setBounds(7, 232, 132, 14);
		OrderDetails.add(lblActualLeavingDate);
		
		ActualLeavingDate = new JTextField();
		ActualLeavingDate.setBounds(121, 229, 68, 20);
		OrderDetails.add(ActualLeavingDate);
		ActualLeavingDate.setColumns(10);
		ActualLeavingDate.setEditable(false);
		
		JLabel label_1 = new JLabel("Actual Arrival Date:");
		label_1.setBounds(7, 204, 132, 14);
		OrderDetails.add(label_1);
		
		ActualArrivalDate = new JTextField();
		ActualArrivalDate.setBounds(121, 201, 68, 20);
		OrderDetails.add(ActualArrivalDate);
		ActualArrivalDate.setColumns(10);
		ActualArrivalDate.setEditable(false);
		
		ArrivalDate = new JTextField();
		ArrivalDate.setBounds(89, 173, 100, 20);
		OrderDetails.add(ArrivalDate);
		ArrivalDate.setColumns(10);
		ArrivalDate.setEditable(false);
		
		JLabel lblArrivalDate = new JLabel("Arrival Date:");
		lblArrivalDate.setBounds(7, 176, 81, 14);
		OrderDetails.add(lblArrivalDate);
		
		JLabel lblCompensation = new JLabel("Leaving Date:");
		lblCompensation.setBounds(7, 148, 81, 14);
		OrderDetails.add(lblCompensation);
		
		LeavingDate = new JTextField();
		LeavingDate.setBounds(82, 145, 107, 20);
		OrderDetails.add(LeavingDate);
		LeavingDate.setColumns(10);
		LeavingDate.setEditable(false);
		
		JLabel lblParkingLotNumber = new JLabel("Parking Lot Number:");
		lblParkingLotNumber.setBounds(7, 123, 132, 14);
		OrderDetails.add(lblParkingLotNumber);
		
		JLabel lblOrderDate = new JLabel("Order Date:");
		lblOrderDate.setBounds(7, 98, 81, 14);
		OrderDetails.add(lblOrderDate);
		
		JLabel lblCarNumber = new JLabel("Car Number:");
		lblCarNumber.setBounds(7, 73, 81, 14);
		OrderDetails.add(lblCarNumber);
		
		JLabel lblWorkerId = new JLabel("Customer ID:");
		lblWorkerId.setBounds(7, 48, 81, 14);
		OrderDetails.add(lblWorkerId);
		
		JLabel lblCustomerId = new JLabel("Order ID:");
		lblCustomerId.setBounds(7, 23, 81, 14);
		OrderDetails.add(lblCustomerId);
		
		ParkingLotNumber = new JTextField();
		ParkingLotNumber.setBounds(129, 117, 60, 20);
		OrderDetails.add(ParkingLotNumber);
		ParkingLotNumber.setColumns(10);
		ParkingLotNumber.setEditable(false);
		
		
		OrderDate = new JTextField();
		OrderDate.setBounds(82, 92, 107, 20);
		OrderDetails.add(OrderDate);
		OrderDate.setColumns(10);
		OrderDate.setEditable(false);
		
		CarNumber = new JTextField();
		CarNumber.setBounds(82, 67, 107, 20);
		OrderDetails.add(CarNumber);
		CarNumber.setColumns(10);
		CarNumber.setEditable(false);
		
		CustomerID = new JTextField();
		CustomerID.setBounds(82, 42, 107, 20);
		OrderDetails.add(CustomerID);
		CustomerID.setColumns(10);
		CustomerID.setEditable(false);
		
		JLabel lblEmail = new JLabel("E-mail:");
		lblEmail.setBounds(7, 257, 104, 14);
		OrderDetails.add(lblEmail);
		
		Email = new JTextField();
		Email.setColumns(10);
		Email.setBounds(48, 254, 141, 20);
		OrderDetails.add(Email);
		Email.setEditable(false);
		
		
		
		
		
		JButton btnCancelOrder = new JButton("cancel order");
		btnCancelOrder.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (row!=-1)
				{
				Date curDate=new Date();
				CustomerID.setText("");
			    OrderID.setText("");
			    CarNumber.setText("");
			    OrderDate.setText("");
			    ArrivalDate.setText("");
				LeavingDate.setText("");
				ActualArrivalDate.setText("");
				ActualLeavingDate.setText("");
				Email.setText("");
				ParkingLotNumber.setText("");
				chckbxNewCheckBox.setSelected(false);
				Order order=o2[row];
				ViewOrCancelOrders.cancelOrder(order);
			}
				else
					JOptionPane.showMessageDialog(null,"choose an order first","Cancel order",JOptionPane.ERROR_MESSAGE);

			
			}
		});
		btnCancelOrder.setBounds(263, 266, 132, 23);
		add(btnCancelOrder);
		
		
		table.addMouseListener(new MouseAdapter() {
			  public void mouseClicked(MouseEvent e) {
				  JTable target = (JTable)e.getSource();
				  row = target.getSelectedRow();
			    if (e.getClickCount() == 2) {
			      Email.setText(o2[row].getEmail());
				  ParkingLotNumber.setText(String.valueOf(o2[row].getParkingLotNumber()));
			      CustomerID.setText(o2[row].getCustomerID());
			      OrderID.setText(String.valueOf(o2[row].getOrderID()));
			      CarNumber.setText(o2[row].getCarNumber());
			      OrderDate.setText(o2[row].getOrderDate().toString());
				  ArrivalDate.setText(o2[row].getArrivalDate().toString());
				  LeavingDate.setText(o2[row].getLeavingDate().toString());
				  if (o2[row].getActualArrivalDate()!=null)
					  ActualArrivalDate.setText(o2[row].getActualArrivalDate().toString());
				  else
					  ActualArrivalDate.setText("");
				  if (o2[row].getActualLeavingDate()!=null)
					  ActualLeavingDate.setText(o2[row].getActualLeavingDate().toString());
				  else
					  ActualLeavingDate.setText("");
				 
				  if (o2[row].isCanceled()==true)
						chckbxNewCheckBox.setSelected(true);
				  else
					  chckbxNewCheckBox.setSelected(false);
						
			    }
			  }
		});
	}
}